Create nodes and relationships in Neo4j using Python

Overview:

  • Nodes and the relationships between them form a graph. There may not be any relationship at all in a graph.
  • In graph theory nodes are also called as vertexes and the relationships are called as edges.
  • In the property graph model provided by Neo4j, nodes and relationships can have a number of labels and properties.
  • A property is a name value pair.
  • The Cypher Query Language (CQL) provides a powerful syntax through which nodes and relationships can be created on a Neo4j database.

Creating graphs on Neo4j database through Python:

  • Using the Neo4j Python driver (neo4j-driver) a Python program can create, manipulate and analyze the graphs on a Neo4j graph database.
  • After importing the GraphDatabase module the first step in connecting to the Neo4j database is to create a driver instance.
  • The driver instance takes care of managing the connection pool required for the application, which can be controlled through parameters like MaxConnectionPoolSize.
  • The Python example program below creates a set of nodes representing some of the Ivy League universities and the distance between them in miles.

Creating nodes and relationships in a Neo4j graph

  • Through the session object obtained from the driver the CQL command is sent to the Neo4j server, which creates the university nodes and the distance relationships between them.

Example:

# import the neo4j driver for Python

from neo4j.v1 import GraphDatabase

 

# Database Credentials

uri             = "bolt://localhost:7687"

userName        = "neo4j"

password        = "test"

 

# Connect to the neo4j database server

graphDB_Driver  = GraphDatabase.driver(uri, auth=(userName, password))

 

# CQL to query all the universities present in the graph

cqlNodeQuery          = "MATCH (x:university) RETURN x"

 

# CQL to query the distances from Yale to some of the other Ivy League universities

cqlEdgeQuery          = "MATCH (x:university {name:'Yale University'})-[r]->(y:university) RETURN y.name,r.miles"

 

# CQL to create a graph containing some of the Ivy League universities

cqlCreate = """CREATE (cornell:university { name: "Cornell University"}),

(yale:university { name: "Yale University"}),

(princeton:university { name: "Princeton University"}),

(harvard:university { name: "Harvard University"}),

 

(cornell)-[:connects_in {miles: 259}]->(yale),

(cornell)-[:connects_in {miles: 210}]->(princeton),

(cornell)-[:connects_in {miles: 327}]->(harvard),

 

(yale)-[:connects_in {miles: 259}]->(cornell),

(yale)-[:connects_in {miles: 133}]->(princeton),

(yale)-[:connects_in {miles: 133}]->(harvard),

 

(harvard)-[:connects_in {miles: 327}]->(cornell),

(harvard)-[:connects_in {miles: 133}]->(yale),

(harvard)-[:connects_in {miles: 260}]->(princeton),

 

(princeton)-[:connects_in {miles: 210}]->(cornell),

(princeton)-[:connects_in {miles: 133}]->(yale),

(princeton)-[:connects_in {miles: 260}]->(harvard)"""

 

# Execute the CQL query

with graphDB_Driver.session() as graphDB_Session:

    # Create nodes

    graphDB_Session.run(cqlCreate)

   

    # Query the graph    

    nodes = graphDB_Session.run(cqlNodeQuery)

   

    print("List of Ivy League universities present in the graph:")

    for node in nodes:

        print(node)

 

    # Query the relationships present in the graph

    nodes = graphDB_Session.run(cqlEdgeQuery)

   

    print("Distance from Yale University to the other Ivy League universities present in the graph:")

    for node in nodes:

        print(node)

 

Output:

List of Ivy League universities present in the graph:

<Record x=<Node id=137 labels={'university'} properties={'name': 'Cornell University'}>>

<Record x=<Node id=138 labels={'university'} properties={'name': 'Yale University'}>>

<Record x=<Node id=139 labels={'university'} properties={'name': 'Princeton University'}>>

<Record x=<Node id=140 labels={'university'} properties={'name': 'Harvard University'}>>

Distance from Yale University to the other Ivy League universities present in the graph:

<Record y.name='Cornell University' r.miles=259>

<Record y.name='Princeton University' r.miles=133>

<Record y.name='Harvard University' r.miles=133>

 


Copyright 2024 © pythontic.com